package com.space.netspider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @Author yangyf
 * @Date 2021/6/9 0009 17:31
 * @Version 1.0
 */
public class TelProcessor implements PageProcessor {
    private List<LawyerBean> lawyerBeanList;
    public TelProcessor(List<LawyerBean> lawyerBeanLis,String regex_URL){
        this.lawyerBeanList = lawyerBeanLis;
        this.regex_URL = regex_URL;
    }
    private static final Logger logger = LoggerFactory.getLogger(TelProcessor.class);
    private Site site = Site.me().setUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36").setRetryTimes(3).setSleepTime(2000).setTimeOut(10000);
    private String regex_URL;
    @Override
    public void process(Page page) {
        // 从分页元素中获取后续链接
        //https://www\.64365\.com/shanxi1/lawyer/page_[0-9]*\.aspx+
        logger.info("Thread:" + Thread.currentThread().getName());
        page.addTargetRequests(page.getHtml().xpath("//div[@class='g-mn ma mt40 tc']/div[@class='u-page']/a/@href").all());
        // 判断是否符合链接格式
        if (page.getUrl().regex(regex_URL).match()) {
            //定义如何抽取页面信息，并保存下来
            List<Selectable> selectableList = page.getHtml().xpath("//ul[@class='law-list']/li[@class='clearfix']").nodes();
            for(Selectable selectable : selectableList){
                LawyerBean tmp = new LawyerBean();
                String name = selectable.xpath("//div[@class='fl']/p/a/text()").toString();
                String goods_tmp = selectable.xpath("//div[@class='fl']/div[@class='mt5 clearfix']/div[@class='w325 fl']/p/text()").toString();
                String goods = "";
                if(goods_tmp != null && goods_tmp.split("：").length > 1){
                    goods = goods_tmp.split("：")[1];
                }
                String company = selectable.xpath("//div[@class='fl']/div[@class='mt5 clearfix']/div[@class='w325 fl']/p[@class='mt5']/a/text()").toString();
                String addr = selectable.xpath("//div[@class='fl']/div[@class='mt5 clearfix']/div[@class='w325 fl']/p[@class='s-c999 mt5']/text()").toString();
                String tel = selectable.xpath("//div[@class='fl']/div[@class='mt5 clearfix']/div[@class='w373 fl']/p[@class='mt15 clearfix']/span[@class='law-tel']/text()").toString();
                tmp.setName(name);
                tmp.setCompany(company);
                tmp.setAddr(addr);
                tmp.setGoods(goods);
                tmp.setTel(tel);
                lawyerBeanList.add(tmp);
            }
        }

    }

    @Override
    public Site getSite() {
        return site;
    }
}
